package com.huarun.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @author hqy
 * @创建时间   2018-10-17 13:53
 * @Description 类描述
 */
public final class WebUtil {

	private WebUtil() {
	}

	public static HttpServletRequest request() {
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
		return request;
	}

	public static HttpSession session() {
		HttpServletRequest request = WebUtil.request();
		return request.getSession();
	}

	/**
	 * 获取客户端的真实IP地址.<br/>
	 * 如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，
	 * 那么取X-Forwarded-For中第一个非unknown的有效IP字符串。
	 * 
	 */
	public static String ipAddr() {
		return WebUtil.ipAddr(WebUtil.request());
	}

	/**
	 * 获取客户端的真实IP地址.<br/>
	 * 如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，
	 * 那么取X-Forwarded-For中第一个非unknown的有效IP字符串。
	 * 
	 * @param request
	 */
	public static String ipAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
			// 多次反向代理后会有多个ip值，第一个ip才是真实ip
			int index = ip.indexOf(",");
			if (index != -1) {
				return ip.substring(0, index);
			} else {
				return ip;
			}
		}
		ip = request.getHeader("X-Real-IP");
		if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
			return ip;
		}
		ip = request.getHeader("Proxy-Client-IP");
		if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
			return ip;
		}
		ip = request.getHeader("WL-Proxy-Client-IP");
		if (StringUtils.isNotBlank(ip) && !"unKnown".equalsIgnoreCase(ip)) {
			return ip;
		}
		return request.getRemoteAddr();
	}

	public static String apiTranslate(MyException e) {
		JSONObject json = new JSONObject();
		json.put(ResultCodeValue.OPER_CODE_KEY, e.getErrorCode());
		json.put(ResultCodeValue.OPER_DESC_KEY, e.getMessage());
		json.put(ResultCodeValue.OPER_TS_KEY, System.currentTimeMillis());
		return json.toJSONString();
	}

	public static String apiTranslate(Exception e) {
		if (e instanceof MyException) {
			return apiTranslate(((MyException) e));
		} else {
			int index = ExceptionUtils.indexOfType(e, MyException.class);
			if (index >= 0) {
				return apiTranslate(((MyException) ExceptionUtils.getThrowableList(e).get(index)));
			} else {
				JSONObject json = new JSONObject();
				json.put(ResultCodeValue.OPER_CODE_KEY, ResultCodeValue.R_UNKNOW);
				json.put(ResultCodeValue.OPER_DESC_KEY, "网络异常");
				json.put(ResultCodeValue.OPER_TS_KEY, System.currentTimeMillis());
				return json.toJSONString();
			}
		}
	}

}
